import java.util.HashMap;
import java.util.Map;

public class Leetcode3 {
    public int lengthOfLongestSubstring(String s) {
        if (s.isEmpty()){
            return 0;
        }
        char[] chars = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        int maxLength = 0;
        int left = 0;
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(chars[i])){
                left = Math.max(map.get(chars[i])+1,left);
            }
                map.put(chars[i],i);
                maxLength = Math.max(maxLength,i-left+1);
        }
        return maxLength;
    }
}
